--TEST--
Check for output api: pdl_write()
--FILE--
<?php
pdl_enable(true);
pdl_watch(PDL_W_STATUS);
pdl_start(true);

// user comstom format functions
function format_record($time, $level, $label, $msg, $data, $file, $line)
{
	return "$time, $level, $label, $msg, " . gettype($data) . ", $file, $line\n";
}

function format_header($from, $period)
{
	$header = "Log header $from and $period\n";
	return $header;
}

//prepare data
function throw_ex($param1, array $param2, $param3) {
	return new Exception("exception message", 133);
}

class TestObjectA {
	private $_private = 'Only me can access myself';
	protected $_protected = 'this is a protected property';
	public $public = array('a', 1);
}

class TestObjectB {
	public $obj_a;
}
$obj_b = new TestObjectB();
$obj_b->obj_a = new TestObjectA();

$multi_string = <<<STR
First line
Second line
So, the end.
STR;

$output_dir = realpath(ini_get('pdlogger.output_dir'));
pdl_format(PDL_FM_TEXT | PDL_FM_XML | PDL_FM_CUSTOM);
pdl_format_handler("format_record", "format_header", "\n\n");

// call record API
pdl_info("test", "info");
pdl_exception("test", throw_ex('string', array(1, 2, 3), 5.5));
pdl_dump("test");
pdl_dump($obj_b->obj_a);
pdl_dump($obj_b);
pdl_dump(array('a', 'b' => 'this is b'));
pdl_dump($multi_string);

pdl_stop(true); //implicit call pdl_write()

$period = (int) ini_get('pdlogger.period');
$time = time();
$time = ($time - ($time % $period));
$text_log_file = $output_dir . "/text/$time.log";
$xml_log_file = $output_dir . "/xml/$time.xml";
$custom_log_file = $output_dir . "/custom/$time";

echo "Precondition check:\n";
echo "output format = " . pdl_format() . "\n";
echo "log period = " . $period . "\n";

echo "\nOutput log:\n";
echo "output file name = $text_log_file\n";
if (is_file($text_log_file)) {
	echo file_get_contents($text_log_file);
	unlink($text_log_file);
}

echo "\n\noutput file name = $xml_log_file\n";
if (is_file($xml_log_file)) {
	echo file_get_contents($xml_log_file);
	unlink($xml_log_file);
}

echo "\n\noutput file name = $custom_log_file\n";
if (is_file($custom_log_file)) {
	echo file_get_contents($custom_log_file);
	unlink($custom_log_file);
}
?>
--EXPECTF--
Precondition check:
output format = 7
log period = 86400

Output log:
output file name = %s/text/%d.log
# Copyright (c) 2012, Zoujie Wu. Generated by PDLogger 1.0.0
#
#   Record from %s
#   Change log after %d sec(s)


@start %d+%d
[%s] [Info] [test] info @%s:%d
[%s] [Exception] [test] Record exception @%s:%d
  Exception: Exception
  Message: exception message
  Throw At: %s:%d
  Trace:
  #0 %s(%d): throw_ex('string', array[3], 5.5)
  #1 {main}
[%s] [Debug] [Dump] Dump debug data @%s:%d
  test
[%s] [Debug] [Dump] Dump debug data @%s:%d
  object(TestObjectA) {
    private $_private = (string) 'Only me can access myself';
    protected $_protected = (string) 'this is a protected property';
    public $public = (array) array[2];
  }
[%s] [Debug] [Dump] Dump debug data @%s:%d
  object(TestObjectB) {
    public $obj_a = (object) TestObjectA;
  }
[%s] [Debug] [Dump] Dump debug data @%s:%d
  array[2] (
    [0] => (string) 'a',
    ['b'] => (string) 'this is b',
  )
[%s] [Debug] [Dump] Dump debug data @%s:%d
  First line
  Second line
  So, the end.
[%s] [Info] [Status] PHP runtime status @Unknown file:0
  - memory usage: %f KB (peak %f KB)
  - time spent: %f msec(s)
  - loaded files: 1


output file name = %s/xml/%d.xml
<?xml version="1.0" encoding="UTF-8"?>
<header>
  <copyright>Copyright (c) 2012, Zoujie Wu</copyright>
  <from>%s</from>
  <period>%d</period>
  <generate>PDLogger 1.0.0</generate>
</header>
<records>
  <start>%d+%d</start>
  <record>
    <time>%s</time>
    <level>Info</level>
    <label>test</label>
    <msg>info</msg>
    <file>%s006.php</file>
    <line>%d</line>
    <data><![CDATA[]]></data>
  </record>
  <record>
    <time>%s</time>
    <level>Exception</level>
    <label>test</label>
    <msg>Record exception</msg>
    <file>%s006.php</file>
    <line>%d</line>
    <data><![CDATA[
Exception: Exception
Message: exception message
Throw At: %s006.php:%d
Trace:
#0 %s006.php(%d): throw_ex('string', array[3], 5.5)
#1 {main}
]]></data>
  </record>
  <record>
    <time>%s</time>
    <level>Debug</level>
    <label>Dump</label>
    <msg>Dump debug data</msg>
    <file>%s006.php</file>
    <line>%d</line>
    <data><![CDATA[
test
]]></data>
  </record>
  <record>
    <time>%s</time>
    <level>Debug</level>
    <label>Dump</label>
    <msg>Dump debug data</msg>
    <file>%s006.php</file>
    <line>%d</line>
    <data><![CDATA[
object(TestObjectA) {
  private $_private = (string) 'Only me can access myself';
  protected $_protected = (string) 'this is a protected property';
  public $public = (array) array[2];
}
]]></data>
  </record>
  <record>
    <time>%s</time>
    <level>Debug</level>
    <label>Dump</label>
    <msg>Dump debug data</msg>
    <file>%s006.php</file>
    <line>%d</line>
    <data><![CDATA[
object(TestObjectB) {
  public $obj_a = (object) TestObjectA;
}
]]></data>
  </record>
  <record>
    <time>%s</time>
    <level>Debug</level>
    <label>Dump</label>
    <msg>Dump debug data</msg>
    <file>%s006.php</file>
    <line>%d</line>
    <data><![CDATA[
array[2] (
  [0] => (string) 'a',
  ['b'] => (string) 'this is b',
)
]]></data>
  </record>
  <record>
    <time>%s</time>
    <level>Debug</level>
    <label>Dump</label>
    <msg>Dump debug data</msg>
    <file>%s006.php</file>
    <line>%d</line>
    <data><![CDATA[
First line
Second line
So, the end.
]]></data>
  </record>
  <record>
    <time>%s</time>
    <level>Info</level>
    <label>Status</label>
    <msg>PHP runtime status</msg>
    <file>Unknown file</file>
    <line>0</line>
    <data><![CDATA[
- memory usage: %f KB (peak %f KB)
- time spent: %f msec(s)
- loaded files: 1
]]></data>
  </record>
</records>


output file name = %s/custom/%d
Log header %d and %d


%f, 1, test, info, NULL, %s006.php, %d
%f, 8, test, Record exception, object, %s006.php, %d
%f, 16, Dump, Dump debug data, string, %s006.php, %d
%f, 16, Dump, Dump debug data, object, %s006.php, %d
%f, 16, Dump, Dump debug data, object, %s006.php, %d
%f, 16, Dump, Dump debug data, array, %s006.php, %d
%f, 16, Dump, Dump debug data, string, %s006.php, %d
%f, 1, Status, PHP runtime status, string, Unknown file, 0
